home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- char selection(char lower, char upper);
- float percentage(float x, float y);
-
- /* simple calculator */
- void main()
- {
- char reply;
- float A, B, result;
-
- printf("Welcome to the calculator\n");
-
- do
- {
- printf("Enter number A ");
- scanf("%f", &A);
-
- printf("Enter number B ");
- scanf("%f", &B);
-
- printf("Which option do you require?\n");
-
- printf("a-addition\nb-subtraction\nc-multiplication\nd-division\ne-percentage\n");
-
- reply=selection('a','e');
-
- switch (reply)
- {
- case 'a':
- result = A + B;
- break;
- case 'b':
- result = A - B;
- break;
- case 'c':
- result = A * B;
- break;
- case 'd':
- if (B==0)
- {
- printf("Division by zero not allowed\n");
- break;
- }
- result = A / B;
- break;
- case 'e':
- result = percentage(A,B);
- break;
- default:
- break;
- }
- printf("\nThe result is %f\n", result);
-
- printf("More Maths?\na-yes\nb-no, quit\n");
-
- }while (selection('a', 'b')=='a');
-
- }
-
- /* define menu function */
- char selection(char lower, char upper)
- {
- char input;
-
- do
- {
- scanf("%1s",&input);
-
- if (input>='A' && input<='Z')
- {
- input=input+32;
- }
-
- }while (input<lower || input>upper);
-
- return input;
- }
-
- /* define percentage function */
- float percentage(float x, float y)
- {
- float result;
-
- result=(x/100.0)*y;
-
- return result;
- }
-
-
-